<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        return $this->view->fetch();
    }
	
	public function caipiao()
	{
		if($this->request->isPost()){
			$params = $this->request->post();
			$today = date('Ymd');

            if(!isset($params['no']) || !$params['no']){
                $this->error('数字错误');
            }
            $no = $params['no'];
            if(count($no) < 1){
                $this->error('数字错误');
            }
            if(!isset($params['multiple']) || !$params['multiple']){
                $this->error('倍数错误');
            }
            $bs = $params['multiple'];//倍数
            if($bs < 1){
                $this->error('倍数错误');
            }

            $numberStr = (string) $no[0];
            $no = str_split($numberStr);
			
			if (count($no) < 3) {
				$this->error('数字错误');
			}

            $type = $params['type'];
            if($type == 1){
                if (count($no) != count(array_unique($no))) {
                    $this->error('不允许有重复数字');
                }
                foreach($no as $v){
                    if($v < 0 || $v > 9){
                        $this->error('数字错误');
                    }
                }
                // 生成所有三位数字组合
                $combinations = $this->generateCombinations(array_values($no));
                foreach($combinations as $v){
                    $number = implode('', $v);
                    //查询是否有相同注数
                    $is_exist = Db::name('caipiao')->where('no',$number)->where('day',$today)->where('type',1)->where('is_del',0)->find();
                    if($is_exist){//如果存在，新增倍数
                        Db::name('caipiao')->where('no',$number)->where('type',1)->where('day',$today)->setInc('multiple',$bs);
                    }else{
                        $insert = [];
                        $insert['no'] = $number;
                        $insert['multiple'] = $bs;
                        $insert['day'] = $today;
                        $insert['createtime'] = time();
                        $insert['type'] = 1;
                        Db::name('caipiao')->insert($insert);
                    }
                }
            }else if($type == 0){

                if($params['is_zh'] == 1){
                    if (count($no) != count(array_unique($no))) {
                        $this->error('不允许有重复数字');
                    }
                    // 生成所有三位数字组合
                    $combinations = $this->get_zh3(array_values($no));
                    foreach($combinations as $v){
                        $number = $v;
//                        dump($v);exit;
                        //查询是否有相同注数
                        $is_exist = Db::name('caipiao')->where('no',$number)->where('day',$today)->where('type',0)->where('is_del',0)->find();
                        if($is_exist){//如果存在，新增倍数
                            Db::name('caipiao')->where('no',$number)->where('type',0)->where('day',$today)->setInc('multiple',$bs);
                        }else{
                            $insert = [];
                            $insert['no'] = $number;
                            $insert['multiple'] = $bs;
                            $insert['day'] = $today;
                            $insert['createtime'] = time();
                            $insert['type'] = 0;
                            Db::name('caipiao')->insert($insert);
                        }
                    }
                }else{
                    if(count($no) != 3){
                        $this->error('数字错误');
                    }
                    $no = implode('', $no);
                    //查询是否有相同注数
                    $is_exist = Db::name('caipiao')->where('no',$no)->where('day',$today)->where('is_del',0)->find();
                    if($is_exist){//如果存在，新增倍数
                        Db::name('caipiao')->where('no',$no)->where('day',$today)->setInc('multiple',$bs);
                    }else{
                        $insert = [];
                        $insert['no'] = $no;
                        $insert['multiple'] = $bs;
                        $insert['day'] = $today;
                        $insert['type'] = 0;
                        $insert['createtime'] = time();
                        Db::name('caipiao')->insert($insert);
                    }
                }

            }elseif($type == 2){
                if (count($no) != count(array_unique($no))) {
                    $this->error('不允许有重复数字');
                }
                foreach($no as $v){
                    if($v < 0 || $v > 9){
                        $this->error('数字错误');
                    }
                }
                // 生成所有三位数字组合
                $combinations = $this->generateArrays(array_values($no));
//                dump($combinations);exit;
                $arr = [];
                foreach($combinations as $v){
                    $number = implode('', $v);

                    if(!in_array($number,$arr)){
                        //查询是否有相同注数
                        $is_exist = Db::name('caipiao')->where('no',$number)->where('day',$today)->where('type',2)->where('is_del',0)->find();
                        if($is_exist){//如果存在，新增倍数
                            Db::name('caipiao')->where('no',$number)->where('type',2)->where('day',$today)->setInc('multiple',$bs);
                        }else{
                            $insert = [];
                            $insert['no'] = $number;
                            $insert['multiple'] = $bs;
                            $insert['day'] = $today;
                            $insert['createtime'] = time();
                            $insert['type'] = 2;
                            Db::name('caipiao')->insert($insert);
                        }
                    }
                    $arr[] = $number;
                }
            }


			$this->success('录入成功');
		}else{
            $data = input();
            $where = [];
            if(isset($data['no']) && $data['no']){
//                $where[] = ['no','=',$data['no']];
            }
			$today = date('Ymd');
			
			$listone = Db::name('caipiao')->where('day',$today)->where($where)->where('is_del',0)->where('type',0)->order('multiple desc')->select();
            $arr1 = [];
            foreach($listone as &$v){
				$numberStr = (string) $v['no'];
				$v['no_arr'] = str_split($numberStr);
                foreach ($v['no_arr'] as $vv){
                    $arr1[] = $vv;
                }
                $arr1[] = (string)$v['multiple'];
			}
            $beishu_count_one = Db::name('caipiao')->where('day',$today)->where($where)->where('is_del',0)->where('type',0)->sum('multiple');
            $this->assign('beishu_count_one',$beishu_count_one);
			$this->assign('listone',$listone);

            $listtwo = Db::name('caipiao')->where('day',$today)->where($where)->where('is_del',0)->where('type',1)->order('multiple desc')->select();
            $arr2 = [];
            foreach($listtwo as &$v){
                $numberStr = (string) $v['no'];
                $v['no_arr'] = str_split($numberStr);
                foreach ($v['no_arr'] as $vv){
                    $arr2[] = $vv;
                }
                $arr2[] = (string)$v['multiple'];
            }

            $list3 = Db::name('caipiao')->where('day',$today)->where($where)->where('is_del',0)->where('type',2)->order('multiple desc')->select();
            $arr3 = [];
            foreach($list3 as &$v){
                $numberStr = (string) $v['no'];
                $v['no_arr'] = str_split($numberStr);
                foreach ($v['no_arr'] as $vv){
                    $arr3[] = $vv;
                }
                $arr3[] = (string)$v['multiple'];
            }
            $beishu_count_two = Db::name('caipiao')->where('day',$today)->where($where)->where('is_del',0)->where('type',1)->sum('multiple');
            $this->assign('beishu_count_two',$beishu_count_two);
            $this->assign('listtwo',$listtwo);

            $beishu_count_three = Db::name('caipiao')->where('day',$today)->where($where)->where('is_del',0)->where('type',2)->sum('multiple');
            $this->assign('beishu_count_three',$beishu_count_three);
            $this->assign('listthree',$list3);
            $this->assign('arr1',json_encode($arr1));
            $this->assign('arr2',json_encode($arr2));
            $this->assign('arr3',json_encode($arr3));
//            $numbers = [1, 2, 3];
//            $res = $this->generateArrays($numbers);
//            dump($res);exit;
			return $this->view->fetch();
		}
		
	}

    // 生成三个数组，每个数组中的数字只能有一个是重复的
    public function generateArrays($numbers) {
        $combinations = [];
        $count = count($numbers);

        // 遍历数组中的每个数字作为重复的数字
        for ($i = 0; $i < $count; $i++) {
            // 为重复的数字选择两个位置
            for ($j = 0; $j < $count; $j++) {
                for ($k = 0; $k < $count; $k++) {
                    // 确保三个位置不是同一个
                    if ($j !== $k && $i !== $j && $i !== $k) {
                        // 构建组合数组
                        $combination = [$numbers[$i], $numbers[$i], $numbers[$j] === $numbers[$k] ? $numbers[$j] : $numbers[$k]];
                        // 对组合进行排序，以确保结果的一致性
                        sort($combination);
                        // 添加到结果数组中
                        $combinations[] = $combination;
                    }
                }
            }
        }

        return $combinations;
    }

    public function get_zh3($numbers) {
       $permutations = []; // 用于存储排列结果的数组

       // 计算输入数字的个数
       $count = count($numbers);

       // 如果数字个数小于3，则直接返回输入字符串作为结果
       if ($count < 3) {
           return false;
       }

       // 生成所有三位数的排列
       for ($i = 0; $i < $count; $i++) {
           for ($j = 0; $j < $count; $j++) {
               for ($k = 0; $k < $count; $k++) {
                   // 确保三个索引互不相同
                   if ($i !== $j && $i !== $k && $j !== $k) {
                       // 创建排列并添加到结果数组中
                       $permutation = $numbers[$i] . $numbers[$j] . $numbers[$k];
                       $permutations[] = $permutation;
                   }
               }
           }
       }

       return $permutations;
   }

    // 定义一个函数来生成所有组合
    public function generateCombinationsOne($numbers) {

        $combinations = [];
        $length = count($numbers);

        // 遍历所有数字以创建三位数组合
        for ($i = 0; $i < $length; $i++) {
            for ($j = 0; $j < $length; $j++) {
                for ($k = 0; $k < $length; $k++) {
                    // 创建组合
                    $combination = $numbers[$i] . $numbers[$j] . $numbers[$k];

                    // 检查是否排除该组合
                    // 排除三个数字都相同的组合
                    if ($numbers[$i] !== $numbers[$j] || $numbers[$j] !== $numbers[$k] || $numbers[$i] !== $numbers[$k]) {
                        // 排除两个数字相同但位置不同的组合
                        if (($numbers[$i] === $numbers[$j] && $i !== $j) || ($numbers[$i] === $numbers[$k] && $i !== $k) || ($numbers[$j] === $numbers[$k] && $j !== $k)) {
                            continue; // 跳过这个组合
                        }

                        // 如果满足条件，添加到组合数组
                        $combinations[] = $combination;
                    }
                }
            }
        }

        return $combinations;
    }

    public function get_list($no,$type)
    {
        $where = [];
        if($no){
            $where['no'] = $no;
        }
        $where['type'] = $type;
        $today = date('Ymd');
        $listone = Db::name('caipiao')->field('no,multiple')->where('day',$today)->where($where)->where('is_del',0)->order('multiple desc')->select();
        $arr = [];
        foreach($listone as $v){
            $str = (string)$v['no'];
            $array = str_split($str);
            foreach ($array as $vv){
                $arr[] = $vv;
            }
            $arr[] = (string)$v['multiple'];
        }
        return json_encode($arr);
    }
	
	//删除当日组合
	public function del_data($type){
		$today = date('Ymd');
		Db::name('caipiao')->where('day',$today)->where('type',$type)->update(['is_del'=>1]);
		$this->success('清除成功');
	}
	public function generateCombinations($numbers, $length = 3) {  
		$combinations = array();  
		$numbersCount = count($numbers);  
	  
		// 如果数组中的数字数量小于3，则无法生成三位数组合  
		if ($numbersCount < 3) {  
			return $combinations;  
		}  
	  
		// 使用递归或循环生成组合  
		for ($i = 0; $i < $numbersCount; $i++) {  
			for ($j = 0; $j < $numbersCount; $j++) {  
				for ($k = 0; $k < $numbersCount; $k++) {  
					// 确保三个索引互不相同，并且排除重复的数字组合  
					if ($i != $j && $i != $k && $j != $k) {  
						$combination = array($numbers[$i], $numbers[$j], $numbers[$k]);  
						// 对组合进行排序，以便将相同的组合视为相同（可选步骤，取决于你是否需要考虑顺序）  
						sort($combination);  
						// 将排序后的组合作为键添加到组合数组中，以自动排除重复项  
						$combinations[implode(',', $combination)] = $combination;  
					}  
				}  
			}  
		}  
	  
		// 重置键，以便返回连续的索引数组  
		$combinations = array_values($combinations);  
	  
		return $combinations;  
	}
}
